package com.zxx.pattern.buildPattern.build_book;

import com.zxx.yuanze.ocp.answer.DoubleCache;
import com.zxx.yuanze.ocp.answer.ImageCache;

/**
 * 这个并不是经典 Builder模式，省略了Director，ConcreteBuilder
 * @author zxx on 2020/12/16
 */
public class ImageLoaderConfig {

    ImageCache imageCache = new DoubleCache();

    //DisplayConfig displayConfig = new DisplayConfig(); //加载图片loading 及 error配置
    //LoaderPolicy policy=new SerialPolicy(); //加载策略

    //线程数量，默认CPU数量+1
    int threadCount = Runtime.getRuntime().availableProcessors() + 1;

    private ImageLoaderConfig() {
    }

    public static class Builder {
        ImageCache imageCache = new DoubleCache();
        //DisplayConfig displayConfig = new DisplayConfig(); //加载图片loading 及 error配置
        //LoaderPolicy policy=new SerialPolicy(); //加载策略
        int threadCount = Runtime.getRuntime().availableProcessors() + 1;

        public Builder setThreadCount(int threadCount) {
            this.threadCount = Math.max(1, threadCount);
            return this;
        }

        public Builder setImageCache(ImageCache imageCache) {
            this.imageCache = imageCache;
            return this;
        }

//        public Builder setLoadingPlaceHolder(int resId) {
//            displayConfig.loadingResId = resId;
//            return this;
//        }
//
//        public Builder setErrorPlaceHolder(int resId) {
//            displayConfig.errorResId = resId;
//            return this;
//        }
//
//        public Builder setLoaderPolicy(LoaderPolicy policy) {
//            if (policy != null) {
//                this.policy = policy;
//            }
//            return this;
//        }

        void applyConfig(ImageLoaderConfig config) {
            config.imageCache = imageCache;
//            config.displayConfig = displayConfig;
//            config.policy = policy;
            config.threadCount = threadCount;
        }


        /**
         * 根据已经配置好的属性创建配置对象
         */
        public ImageLoaderConfig create() {
            ImageLoaderConfig config = new ImageLoaderConfig();
            //应用配置
            applyConfig(config);
            return config;
        }

    }


}
